import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/model/setting_user_header_model.dart';
import 'package:love_app/model/we_info_model.dart';
import 'package:love_app/providers/share_provider.dart';
import 'package:love_app/routes/application.dart';
import 'package:love_app/service/add_recore_api.dart';
import 'package:love_app/service/setting_info_api.dart';
import 'package:love_app/service/we_info_api.dart';
import 'package:provider/provider.dart';

class WeInfo extends StatefulWidget {
  @override
  _WeInfoState createState() => _WeInfoState();
}

class _WeInfoState extends State<WeInfo> {
  ShareProvider _shareProvider;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _shareProvider = Provider.of<ShareProvider>(context, listen: false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Su().appBar("我们的信息", colors: _shareProvider.themeColor),
      body: _body(),
    );
  }

  Widget _body() {
    return FutureBuilder(
      future: _getWeInfo(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          WeInfoModel model = snapshot.data;
          return _content(model);
        }
        return Container();
      },
    );
  }

  // 发起网络请求
  Future<WeInfoModel> _getWeInfo() async {
    WeInfoModel model = await WeInfoApi().getWeInfo(_shareProvider.token);
    return model;
  }

  // 页面内容
  Widget _content(WeInfoModel weInfoModel) {
    return Container(
      child: Scrollbar(
        child: SingleChildScrollView(
          child: _columnWidge(weInfoModel),
        ),
      ),
    );
  }

  Widget _columnWidge(WeInfoModel weInfoModel) {
    return Column(
      children: <Widget>[
        // 第一个盒子内容
        Container(
          child: _first(weInfoModel),
          margin: EdgeInsets.only(
            top: Su().h(20),
            bottom: Su().h(18),
          ),
        ),
        _second(weInfoModel),
        Container(
          margin: EdgeInsets.only(
            top: Su().h(21),
          ),
          child: _buildItem(
              "恋爱宣言", weInfoModel.data.relationInfo.loveDeclaration ?? ""),
        ),
      ],
    );
  }

  // 第一个盒子内容
  Widget _first(WeInfoModel weInfoModel) {
    int lvoeTime = weInfoModel.data.relationInfo.loveDay;
    String lvoeTimeString = Su().timestampToTime(lvoeTime, cut: "/");
    int birthday = weInfoModel.data.myInfo.birthday;
    String birthdayString = Su().timestampToTime(birthday, cut: "/");
    return Column(
      children: <Widget>[
        _buildItem("相恋时间", lvoeTimeString),
        _buildHeaderItem("我的头像", weInfoModel.data.myInfo.headimgurl, my: true),
        _buildItem("我的昵称", weInfoModel.data.myInfo.nickname,
            route: "settingMyName"),
        _buildItem("我的生日", birthdayString),
        _line(),
      ],
    );
  }

  // 第二个盒子内容
  Widget _second(WeInfoModel weInfoModel) {
    int lvoebirthday = weInfoModel.data.loverInfo.birthday;
    String lvoeTimeString = Su().timestampToTime(lvoebirthday, cut: "/");
    return Column(
      children: <Widget>[
        _buildHeaderItem("另一半的头像", weInfoModel.data.loverInfo.headimgurl),
        _buildItem("另一半的呢陈", weInfoModel.data.loverInfo.nickname),
        _buildItem("另一半的生日", lvoeTimeString),
        _line(),
      ],
    );
  }

  // 构造头像item
  Widget _buildHeaderItem(String leftString, String headerImage,
      {bool my = false}) {
    return InkWell(
      onTap: () async {
        if (my == true) {
          var imageFile =
              await ImagePicker.pickImage(source: ImageSource.gallery);
          if (imageFile != null) {
            String qiniuKey = await AddRecordApi()
                .uploadOne(_shareProvider.token, imageFile.path);
            SettingUserHeaderModel model = await SeetingInfoApi()
                .settingUserHeader(_shareProvider.token, qiniuKey);
            _shareProvider.headerImage = model.data.thumbnail;
            setState(() {});
          }
        }
      },
      child: Container(
        height: Su().h(99),
        width: Su().w(750),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            top: BorderSide(
              width: Su().w(1),
              color: Color.fromRGBO(224, 224, 224, 1),
            ),
          ),
        ),
        padding: EdgeInsets.only(
          left: Su().w(30),
          right: Su().w(49),
        ),
        child: _buildHeaderItemRow(headerImage, leftString),
      ),
    );
  }

  // 构造头像item的Row
  Widget _buildHeaderItemRow(String headerImage, String leftString) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Text(
          leftString,
          style: Su().textStyle(Color.fromRGBO(38, 38, 38, 1), 30),
        ),
        ClipOval(
          child: Image.network(
            headerImage,
            fit: BoxFit.cover,
            width: Su().w(87),
            height: Su().w(87),
          ),
        ),
      ],
    );
  }

  // 构造不包括头像的item
  Widget _buildItem(String leftString, String rightData, {String route}) {
    return InkWell(
      onTap: () {
        if (route != null) {
          Application.router.navigateTo(context, "/$route");
        }
      },
      child: Container(
        height: Su().h(99),
        width: Su().w(750),
        decoration: BoxDecoration(
          color: Colors.white,
          border: Border(
            top: BorderSide(
              width: Su().w(1),
              color: Color.fromRGBO(224, 224, 224, 1),
            ),
          ),
        ),
        padding: EdgeInsets.only(
          left: Su().w(30),
          right: Su().w(49),
        ),
        child: _buildItemRow(leftString, rightData),
      ),
    );
  }

  // 构造不包括头像的item的Row
  Widget _buildItemRow(String leftString, String rightData) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Expanded(
          child: Text(
            leftString,
            style: Su().textStyle(Color.fromRGBO(38, 38, 38, 1), 30),
          ),
        ),
        Container(
          child: Text(
            rightData,
            style: Su().textStyle(Color.fromRGBO(179, 179, 179, 1), 28),
          ),
          padding: EdgeInsets.only(
            right: Su().w(19),
          ),
        ),
        Icon(
          Icons.navigate_next,
          color: Color.fromRGBO(179, 179, 179, 1),
        ),
      ],
    );
  }

  // 一条线
  Widget _line() {
    return Container(
      color: Color.fromRGBO(224, 224, 224, 1),
      width: Su().w(1),
    );
  }
}

// BuildContext _context;
// ShareProvider _shareProvider;

// // 我们的信息页面
// class WeInfos extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     _context = context;
//     _shareProvider = Provider.of<ShareProvider>(context, listen: false);
//     return Scaffold(
//       appBar: Su().appBar("我们的信息", colors: _shareProvider.themeColor),
//       body: _body(_shareProvider),
//     );
//   }

//   Widget _body(ShareProvider shareProvider) {
//     return FutureBuilder(
//       future: _getWeInfo(shareProvider.token),
//       builder: (BuildContext context, AsyncSnapshot snapshot) {
//         if (snapshot.connectionState == ConnectionState.done) {
//           WeInfoModel model = snapshot.data;
//           return _content(model);
//         }
//         return Container();
//       },
//     );
//   }

//   // 发起网络请求
//   Future<WeInfoModel> _getWeInfo(String token) async {
//     WeInfoModel model = await WeInfoApi().getWeInfo(token);
//     return model;
//   }

//   // 页面内容
//   Widget _content(WeInfoModel weInfoModel) {
//     return Container(
//       child: Scrollbar(
//         child: SingleChildScrollView(
//           child: _columnWidge(weInfoModel),
//         ),
//       ),
//     );
//   }

//   Widget _columnWidge(WeInfoModel weInfoModel) {
//     return Column(
//       children: <Widget>[
//         // 第一个盒子内容
//         Container(
//           child: _first(weInfoModel),
//           margin: EdgeInsets.only(
//             top: Su().h(20),
//             bottom: Su().h(18),
//           ),
//         ),
//         _second(weInfoModel),
//         Container(
//           margin: EdgeInsets.only(
//             top: Su().h(21),
//           ),
//           child: _buildItem(
//               "恋爱宣言", weInfoModel.data.relationInfo.loveDeclaration ?? ""),
//         ),
//       ],
//     );
//   }

//   // 第一个盒子内容
//   Widget _first(WeInfoModel weInfoModel) {
//     int lvoeTime = weInfoModel.data.relationInfo.loveDay;
//     String lvoeTimeString = Su().timestampToTime(lvoeTime, cut: "/");
//     int birthday = weInfoModel.data.myInfo.birthday;
//     String birthdayString = Su().timestampToTime(birthday, cut: "/");
//     return Column(
//       children: <Widget>[
//         _buildItem("相恋时间", lvoeTimeString),
//         _buildHeaderItem("我的头像", weInfoModel.data.myInfo.headimgurl, my: true),
//         _buildItem("我的昵称", weInfoModel.data.myInfo.nickname,
//             route: "settingMyName"),
//         _buildItem("我的生日", birthdayString),
//         _line(),
//       ],
//     );
//   }

//   // 第二个盒子内容
//   Widget _second(WeInfoModel weInfoModel) {
//     int lvoebirthday = weInfoModel.data.loverInfo.birthday;
//     String lvoeTimeString = Su().timestampToTime(lvoebirthday, cut: "/");
//     return Column(
//       children: <Widget>[
//         _buildHeaderItem("另一半的头像", weInfoModel.data.loverInfo.headimgurl),
//         _buildItem("另一半的呢陈", weInfoModel.data.loverInfo.nickname),
//         _buildItem("另一半的生日", lvoeTimeString),
//         _line(),
//       ],
//     );
//   }

//   // 构造头像item
//   Widget _buildHeaderItem(String leftString, String headerImage,
//       {bool my = false}) {
//     return InkWell(
//       onTap: () async {
//         if (my == true) {
//           var imageFile =
//               await ImagePicker.pickImage(source: ImageSource.gallery);
//           String qiniuKey = await AddRecordApi()
//               .uploadOne(_shareProvider.token, imageFile.path);
//           SettingUserHeaderModel model = await SeetingInfoApi()
//               .settingUserHeader(_shareProvider.token, qiniuKey);
//           _shareProvider.headerImage = model.data.thumbnail;
//         }
//       },
//       child: Container(
//         height: Su().h(99),
//         width: Su().w(750),
//         decoration: BoxDecoration(
//           color: Colors.white,
//           border: Border(
//             top: BorderSide(
//               width: Su().w(1),
//               color: Color.fromRGBO(224, 224, 224, 1),
//             ),
//           ),
//         ),
//         padding: EdgeInsets.only(
//           left: Su().w(30),
//           right: Su().w(49),
//         ),
//         child: _buildHeaderItemRow(headerImage, leftString),
//       ),
//     );
//   }

//   // 构造头像item的Row
//   Widget _buildHeaderItemRow(String headerImage, String leftString) {
//     return Row(
//       mainAxisAlignment: MainAxisAlignment.spaceBetween,
//       crossAxisAlignment: CrossAxisAlignment.center,
//       children: <Widget>[
//         Text(
//           leftString,
//           style: Su().textStyle(Color.fromRGBO(38, 38, 38, 1), 30),
//         ),
//         ClipOval(
//           child: Image.network(
//             headerImage,
//             width: Su().w(87),
//             height: Su().w(89),
//           ),
//         ),
//       ],
//     );
//   }

//   // 构造不包括头像的item
//   Widget _buildItem(String leftString, String rightData, {String route}) {
//     return InkWell(
//       onTap: () {
//         if (route != null) {
//           Application.router.navigateTo(_context, "/$route");
//         }
//       },
//       child: Container(
//         height: Su().h(99),
//         width: Su().w(750),
//         decoration: BoxDecoration(
//           color: Colors.white,
//           border: Border(
//             top: BorderSide(
//               width: Su().w(1),
//               color: Color.fromRGBO(224, 224, 224, 1),
//             ),
//           ),
//         ),
//         padding: EdgeInsets.only(
//           left: Su().w(30),
//           right: Su().w(49),
//         ),
//         child: _buildItemRow(leftString, rightData),
//       ),
//     );
//   }

//   // 构造不包括头像的item的Row
//   Widget _buildItemRow(String leftString, String rightData) {
//     return Row(
//       mainAxisAlignment: MainAxisAlignment.spaceBetween,
//       crossAxisAlignment: CrossAxisAlignment.center,
//       children: <Widget>[
//         Expanded(
//           child: Text(
//             leftString,
//             style: Su().textStyle(Color.fromRGBO(38, 38, 38, 1), 30),
//           ),
//         ),
//         Container(
//           child: Text(
//             rightData,
//             style: Su().textStyle(Color.fromRGBO(179, 179, 179, 1), 28),
//           ),
//           padding: EdgeInsets.only(
//             right: Su().w(19),
//           ),
//         ),
//         Icon(
//           Icons.navigate_next,
//           color: Color.fromRGBO(179, 179, 179, 1),
//         ),
//       ],
//     );
//   }

//   // 一条线
//   Widget _line() {
//     return Container(
//       color: Color.fromRGBO(224, 224, 224, 1),
//       width: Su().w(1),
//     );
//   }
// }
